/*
 * ACC.c
 *
 *  Created on: 28.02.2011
 *      Author: Tavin
 */

#include <stdint.h>

#include "board.h"

#include "USART.h"
#include "gpio.h"
#include "INTC.h"

#include "acc.h"
#include "System.h"


static const usart_spi_options_t USART_SPI_ACC_OPTIONS=
{
  .baudrate = 1000000,
  .charlength = 8,
  .spimode = 0,
  .channelmode =USART_NORMAL_CHMODE
};
static const gpio_map_t USART_ACC_GPIO_MAP ={
		{ACC_CLK_PIN, ACC_CLK_FUNCTION},
		{ACC_TX_PIN, ACC_TX_FUNCTION},
		{ACC_RX_PIN, ACC_RX_FUNCTION},
		{ACC_SS_PIN, ACC_SS_FUNCTION}
};




AccData_t accData;

#define ACC_X_AXIS 0
#define ACC_Y_AXIS 1
#define ACC_Z_AXIS 2

#if __GNUC__
__attribute__((__interrupt__))
#elif __ICCAVR32__
#pragma handler = AVR32_PDCA_IRQ_GROUP, 0
__interrupt
#endif
static void ACC_int_handler(void)
{


	accData.chanelData[accData.activeChanel][accData.dataPointer++]=ACC_USART->rhr;

	if (accData.dataPointer>1)
	{
		usart_spi_unselectChip(ACC_USART);
		ACC_USART->idr=-1;

		accData.activeChanel++;
		if (accData.activeChanel>2)
			accData.activeChanel=0;

		accData.dataPointer=0;
	}
	else
	{
		ACC_USART->thr=0;
	}

}


/****************************************************************************************/
/*																						*/
/*	Function : ACC_init										Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.3.1			Status : Working   			*/
/*																						*/
/****************************************************************************************/
void ACC_init()
{
	gpio_enable_module(USART_ACC_GPIO_MAP,
			sizeof(USART_ACC_GPIO_MAP) / sizeof(USART_ACC_GPIO_MAP[0]));

	usart_init_spi_master((ACC_USART), &USART_SPI_ACC_OPTIONS,PERIPHERIAL_CLOCK);


	usart_spi_selectChip(ACC_USART);

	usart_putchar(ACC_USART,0xAc);
	usart_putchar(ACC_USART,0x5);
	usart_spi_unselectChip(ACC_USART);

	Disable_global_interrupt();
	INTC_register_interrupt( (__int_handler) &ACC_int_handler, ACC_IRQ, AVR32_INTC_INT2);


	//pdca_enable_interrupt_transfer_complete(DISPLAY_PDCA_CHANNEL);

	Enable_global_interrupt();
}



/****************************************************************************************/
/*																						*/
/*	Function : Acc_on										Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.5.22			Status : Working   			*/
/*																						*/
/****************************************************************************************/
void Acc_on()
{
	uint8_t counter;
	//for (counter=0;counter<)
	//accData.chanelData[accData.activeChanel][accData.dataPointer++]
}

/****************************************************************************************/
/*																						*/
/*	Function : Acc_off										Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.5.22			Status : Working   			*/
/*																						*/
/****************************************************************************************/
void Acc_off()
{




	Acc_clearData();

}

/****************************************************************************************/
/*																						*/
/*	Function : Acc_aquireData								Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.3.1			Status : Working   			*/
/*																						*/
/****************************************************************************************/
void Acc_aquireData()
{
	if (System_isAccOn()==TRUE)
		{
		ACC_USART->ier=AVR32_USART_IER_RXRDY_MASK;
			usart_spi_selectChip(ACC_USART);

			ACC_USART->thr=(6+accData.activeChanel)<<1;
		}

}


/****************************************************************************************/
/*																						*/
/*	Function : Acc_aquireData								Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.5.22			Status : Working   			*/
/*																						*/
/****************************************************************************************/
void Acc_clearData()
{
	uint8_t counter;
	for (counter=0;counter<3 ;counter++)
		{
		accData.chanelData[0][counter]=0;
		accData.chanelData[1][counter]=0;
		accData.chanelData[2][counter]=0;

		}
}



/****************************************************************************************/
/*																						*/
/*	Function : ACC_getAxisData								Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.3.1			Status : Working   			*/
/*																						*/
/****************************************************************************************/
int8_t ACC_getAxisData(uint8_t axis)
{
	return(accData.chanelData[axis][1]);
}

